#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qpainter.h"
#include "qevent.h"
#include "qpoint.h"
#include "qmessagebox.h"




MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	ui->mainToolBar->hide();
	ui->menuBar->hide();

	resize((CHESS_COLUMES + 1)*RECT_WIDTH  ,(CHESS_ROWS + 1)*RECT_HEIGHT);

	mIsBlackTurn = true;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *e)
{
	DrawChessboard();		
    DrawPiece();			
    DrawPieceWithMouse();	

	update();
}

void MainWindow::DrawChessboard()
{
	QPainter painter(this);
    painter.setBrush(QColorConstants::Svg::pink);
    painter.setPen(QPen(QColor(Qt::red),2));

	for(int i = 0;i<CHESS_COLUMES; i++)
	{
		for (int j = 0; j<CHESS_ROWS; j++)
		{
			painter.drawRect( (i+0.5)*RECT_WIDTH,(j+0.5)*RECT_HEIGHT,RECT_WIDTH,RECT_HEIGHT);
		}
	}
}

void MainWindow::DrawPiece()
{
	QPainter painter(this);
	painter.setPen(QPen(QColor(Qt::transparent)));

    for (int i = 0; i<mPiece.size(); i++)
	{
        Piece piece= mPiece[i];
        if (piece.mBlack)
		{
            painter.setBrush(QColorConstants::Svg::bisque);
		}
		else
        {
                  painter.setBrush(QColorConstants::Svg::aqua);
                }
        DrawChessAtPoint(painter,piece.mPt);
	}
}

void MainWindow::DrawChessAtPoint(QPainter& painter,QPoint& pt)
{

	QPoint ptCenter((pt.x()+0.5)*RECT_WIDTH,(pt.y()+0.5)*RECT_HEIGHT);
	painter.drawEllipse(ptCenter,RECT_WIDTH / 2,RECT_HEIGHT / 2);
}

void MainWindow::DrawPieceWithMouse()
{
	QPainter painter(this);
	painter.setPen(QPen(QColor(Qt::transparent)));

	if (mIsBlackTurn)
	{
        painter.setBrush(QColorConstants::Svg::bisque);
	}
	else
	{
        painter.setBrush(QColorConstants::Svg::aqua);
	}

	painter.drawEllipse(mapFromGlobal(QCursor::pos()),RECT_WIDTH / 2,RECT_HEIGHT / 2);
	
}

void MainWindow::mousePressEvent(QMouseEvent * e)
{
	
	QPoint pt;
	pt.setX( (e->pos().x() ) / RECT_WIDTH);
	pt.setY( (e->pos().y() ) / RECT_HEIGHT);

	
    for (int i = 0; i<mPiece.size(); i++)
	{
        Piece piece= mPiece[i];
        if (piece.mPt == pt)
		{
			
			return;
		}
	}
	
    Piece piece(pt,mIsBlackTurn);
    mPiece.append(piece);

	
    int nLeft =			CountNearItem(piece,QPoint(-1,0));
    int nLeftUp =		CountNearItem(piece,QPoint(-1,-1));
    int nUp =			CountNearItem(piece,QPoint(0,-1));
    int nRightUp =		CountNearItem(piece,QPoint(1,-1));
    int nRight =		CountNearItem(piece,QPoint(1,0));
    int nRightDown =	CountNearItem(piece,QPoint(1,1));
    int nDown =			CountNearItem(piece,QPoint(0,1));
    int nLeftDown =		CountNearItem(piece,QPoint(-1,1));
	if ( (nLeft + nRight) >= 4 ||
		 (nLeftUp + nRightDown) >= 4 ||
		 (nUp + nDown) >= 4 ||
		 (nRightUp + nLeftDown) >= 4 )
	{
        QString str = mIsBlackTurn?"BisquaWinTheGame":"AquaWinTheGame";
		QMessageBox::information(NULL,  "GAME OVER",str, QMessageBox::Yes , QMessageBox::Yes);
        mPiece.clear();
		
		return;
	}
	
	mIsBlackTurn = !mIsBlackTurn;
}

int MainWindow::CountNearItem(Piece piece,QPoint ptDirection)
{
	int nCount = 0;
    piece.mPt += ptDirection;

    while (mPiece.contains(piece))
	{
		nCount++;
        piece.mPt += ptDirection;
	}
	return nCount;
}
